﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using IPhonePlugin;

namespace HoanLongPhonePlugin
{
    public class HoanLongPhonePlugin:IPhonePlugin.IPhonePlugin
    {
        public List<PhoneInfo> getPhone(string name, int nItem)
        {
            var webGet = new HtmlWeb();
            string temp = name.Trim();
            temp=temp.Replace(' ','+');
            List<PhoneInfo> listPhone = new List<PhoneInfo>();
            string url = "http://hoanlong.com.vn/index.php?option=com_ecommerce&view=list&keyword=Iphone+4s&catid=305&giatu=0&dengia=0&issearch=1&catid_=305&Itemid=117&button=+&K2ExtraField_52=&K2ExtraField_93=&K2ExtraField_95=&K2ExtraField_96=&K2ExtraField_98=&K2ExtraField_103=&K2ExtraField_59=&K2ExtraField_82=&K2ExtraField_107=&K2ExtraField_61=&K2ExtraField_92=&K2ExtraField_109=";
            var document = webGet.Load(url);
            if (document != null)
            {
                HtmlNodeCollection Nodes = document.DocumentNode.SelectNodes("//div[@class='wapper_sp_1']");                
                if (Nodes != null && Nodes.Count > 0)
                {
                    int n = Nodes.Count;
                    if (n > nItem)
                    {
                        n = nItem;
                    }
                    for (int i = 0; i < n; i++)
                    {
                        PhoneInfo PhoneInfo = new PhoneInfo();
                        PhoneInfo.CuaHang = "Hoàn Long";

                        HtmlNode nodeTen = Nodes[i].SelectSingleNode("child::div[@class='wapper_sp_1_title']/a");
                        if (nodeTen != null && nodeTen.InnerText != null)
                            PhoneInfo.TenSanPham = nodeTen.InnerText.Trim();

                        HtmlNode nodeGia = Nodes[i].SelectSingleNode("child::div[@class='wapper_sp_1_price']/span");
                        if (nodeGia != null && nodeGia.InnerText != null)
                            PhoneInfo.GiaBan = nodeGia.InnerText.Trim();

                        listPhone.Add(PhoneInfo);
                    }
                }
            }

            return listPhone;
        }
    }
}
